const app = getApp()
let qCloud = require("../../index.js");

Page({
  /** 页面的初始数据 */
  data: {
    imgPath: qCloud.config.STATIC_IMAGE_PATH,
    text: "服饰生产通：服装企业协同办公软件，企业间数据互联...",
    task: {
      processTask: 0, //生产中
      completeTask: 0, //已完成
      pendingApproval: 0, //待审核
      goodsTotal: 0, //货物数量
      goodsInventory: 0, //剩余仓库
      staffTotal: 0, //员工人数
      currentMonthTotalWages: 0, //本月累计工资:type==1时必输
      currentMonthIssuedWages: 0, //本月已发放工资:type==1时必输
      currentMonthWages: 0, //本月工资:type==2时必输
      upcountTotal: 0, //上报数量:type==2时必输
      pendingReview: 0, //待审核
      reviewed: 0, //已审核:type==2时必输
    },

    marqueePace: 0.8, //滚动速度
    marqueeDistance: 0, //初始滚动距离
    marqueeDistance2: 0,
    marquee2copy_status: false,
    marquee2_margin: 600,
    size: 14,
    orientation: "left", //滚动方向
    interval: 30, // 时间间隔

    produce: false,
    approval: false,
    warehouse: false,
    taskReport: false,
    wages: false,
    mywages: false,
    auth: false,
    procedure: false,
    user: false,
    quick: false,

    userInfo: {},
    status: '0',
    showModalStatus: false,
  },

  /** 生命周期函数--监听页面加载 */
  onLoad: function (options) {
    this.getUserInfo();
    this.getPermission();

    let userInfo = qCloud.session.getUser();
    if (userInfo != null) {
      this.setData({
        userInfo: userInfo,
        status: userInfo.status
      });

      if (userInfo.status == '0') {
        let length = this.data.text.length * this.data.size; //文字长度
        let windowWidth = wx.getSystemInfoSync().windowWidth; // 屏幕宽度
        this.setData({
          length: length,
          windowWidth: windowWidth,
          marquee2_margin: length < windowWidth ? windowWidth - length : this.data.marquee2_margin //当文字长度小于屏幕长度时，需要增加补白
        });
        this.run2(); // 第一个字消失后立即从右边出现

        this.queryBusinessStatistic();
        this.getNoticeList();
      } else if (userInfo.status == '2') {
        wx.showModal({
          title: "提示",
          content: '您在该企业当前状态（已离职）',
          confirmText: '加入企业',
          complete: (res) => {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/my/info/join',
              })
            }
          }
        })
      } else if (userInfo.status == '3') {
        wx.showModal({
          title: "提示",
          content: '企业审核中，请耐心等待',
          complete: (res) => {

          }
        })
      }
    }
  },

  bindSearchTap: function (e) {
    wx.navigateTo({
      url: '/pages/index/search'
    })
  },

  bindGoTo: function (e) {
    if (e.currentTarget.dataset.index == '1') {
      wx.navigateTo({
        url: '/pages/produce/index'
      })
    }

    if (e.currentTarget.dataset.index == '2') {
      wx.navigateTo({
        url: '/pages/examine/lists'
      })
    }

    if (e.currentTarget.dataset.index == '3') {
      wx.navigateTo({
        url: '/pages/warehouse/index'
      })
    }

    if (e.currentTarget.dataset.index == '4') {
      wx.navigateTo({
        url: '/pages/wages/index'
      })
    }

    if (e.currentTarget.dataset.index == '5') {
      wx.navigateTo({
        url: '/pages/report/index?tab=1'
      })
    }

    if (e.currentTarget.dataset.index == '6') {
      wx.navigateTo({
        url: '/pages/wages/my'
      })
    }

    if (e.currentTarget.dataset.index == '7') {
      wx.navigateTo({
        url: '/pages/produce/adds'
      })
    }

    if (e.currentTarget.dataset.index == '8') {
      wx.navigateTo({
        url: '/pages/enterprise/procedure/index'
      })
    }

    if (e.currentTarget.dataset.index == '9') {
      wx.navigateTo({
        url: '/pages/enterprise/auth/index'
      })
    }
  },

  // 查询业务统计
  queryBusinessStatistic() {
    let self = this;
    let params = {};
    app.fetchApis(self, qCloud.api.common_queryBusinessStatistic, params, "POST", function (e) {
      let result = e.data;
      if (result.code == 200) {
        self.setData({
          task: result.data
        });
      }
    });
  },

  // 获取系统公告
  getNoticeList() {
    let self = this;
    let params = {
      pageNum: 1,
      pageSize: 1,
      noticeType: "2", // 公告类型: 1-通知; 2-公告;
    };

    app.fetchApis(self, qCloud.api.getNoticeList, params, "GET", function (e) {
      let result = e.data;
      if (result.code == 200) {
        if (result.rows.length > 0) {
          self.setData({
            text: result.rows[0].noticeContent
          });
        }
      }
    });
  },

  // 打开扫码
  scanCode: function () {
    let self = this;
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ['barCode', 'qrCode'],
      success: function (res) {
        app.fetchApis(self, qCloud.api.producetask_checkAuthority, { produceTaskId: res.result }, "POST", function (e) {
          if (e.data.code == 200) {
            if (qCloud.session.getUser().kindType == '4') {
              wx.navigateTo({
                url: '/pages/report/upcount?produceTaskId=' + res.result
              })
            }
            if (qCloud.session.getUser().kindType == '2') {
              wx.navigateTo({
                url: '/pages/produce/upcount?produceTaskId=' + res.result
              })
            }
          }
        });
      },
      fail(res) {

      }
    });
  },

  /** 生命周期函数--监听页面显示 */
  onShow: function () {
    this.getUserInfo();
    this.getPermission();

    let userInfo = qCloud.session.getUser();
    if (userInfo != null) {
      wx.setNavigationBarTitle({
        title: userInfo.organName
      })

      this.setData({
        userInfo: userInfo,
        status: userInfo.status
      });

      if (userInfo.status == '0') {
        this.queryBusinessStatistic();
        this.getNoticeList();
      }
    }
  },

  run2: function () {
    let vm = this;
    let interval = setInterval(function () {
      if (-vm.data.marqueeDistance2 < vm.data.length) {
        // 如果文字滚动到出现marquee2_margin=30px的白边，就接着显示
        vm.setData({
          marqueeDistance2: vm.data.marqueeDistance2 - vm.data.marqueePace,
          marquee2copy_status: vm.data.length + vm.data.marqueeDistance2 <= vm.data.windowWidth + vm.data.marquee2_margin,
        });
      } else {
        if (-vm.data.marqueeDistance2 >= vm.data.marquee2_margin) { // 当第二条文字滚动到最左边时
          vm.setData({
            marqueeDistance2: vm.data.marquee2_margin // 直接重新滚动
          });
          clearInterval(interval);
          vm.run2();
        } else {
          clearInterval(interval);
          vm.setData({
            marqueeDistance2: -vm.data.windowWidth
          });
          vm.run2();
        }
      }
    }, vm.data.interval);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getUserInfo();
    this.getPermission();

    let userInfo = qCloud.session.getUser();
    if (userInfo != null) {
      this.setData({
        userInfo: userInfo,
        status: userInfo.status
      });

      if (userInfo.status == '0') {
        this.queryBusinessStatistic();
        this.getNoticeList();
      }
    }
  },

  getUserInfo: function () {
    let self = this;
    app.fetchApis(self, qCloud.api.getInfo, {}, "GET", function (e) {
      if (e.data.code == 200) {
        qCloud.session.setPermi(e.data.permissions);
        qCloud.session.setRole(e.data.role);
        qCloud.session.setUser(e.data.user);
      }
    });
  },

  getPermission: function () {
    let self = this;
    let permission = qCloud.session.getPermi();
    if (permission && permission.length > 0) {
      if (permission.indexOf("apc:produce:list") != -1) {
        self.setData({
          produce: true
        });
      } else {
        self.setData({
          produce: false
        });
      }

      if (permission.indexOf("apc:produce:add") != -1) {
        self.setData({
          btn0: true
        });
      } else {
        self.setData({
          btn0: false
        });
      }

      if (permission.indexOf("apc:approval:list") != -1) {
        self.setData({
          approval: true
        });
      } else {
        self.setData({
          approval: false
        });
      }

      if (permission.indexOf("apc:warehouse:list") != -1) {
        self.setData({
          warehouse: true
        });
      } else {
        self.setData({
          warehouse: false
        });
      }

      if (permission.indexOf("apc:wages:list") != -1) {
        self.setData({
          wages: true
        });
      } else {
        self.setData({
          wages: false
        });
      }

      if (permission.indexOf("apc:mywages:list") != -1) {
        self.setData({
          mywages: true
        });
      } else {
        self.setData({
          mywages: false
        });
      }

      if (permission.indexOf("apc:report:list") != -1) {
        self.setData({
          taskReport: true
        });
      } else {
        self.setData({
          taskReport: false
        });
      }

      if (permission.indexOf("preauth:apcUser:list") != -1) {
        self.setData({
          user: true
        });
      } else {
        self.setData({
          user: false
        });
      }

      if (permission.indexOf("apc:procedure:list") != -1) {
        self.setData({
          procedure: true
        });
      } else {
        self.setData({
          procedure: false
        });
      }

      if (self.data.btn0 || self.data.user || self.data.procedure) {
        self.setData({
          quick: true
        });
      } else {
        self.setData({
          quick: false
        });
      }
    } else {
      self.setData({
        produce: false,
        approval: false,
        warehouse: false,
        taskReport: false,
        wages: false,
        mywages: false,
        auth: false,
        procedure: false,
        user: false,
        quick: false
      })
    }
  }
});
